package com.example.xd.tuiliu_test.rtp;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xd.tuiliu_test.R;
import com.example.xd.tuiliu_test.rtp.android.screenrecorder.rtp.RtpSenderWrapper;
import com.example.xd.tuiliu_test.rtp.encode.androidencode.AvcEncoder;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

public class RtpPushActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback {
    DatagramSocket socket;
    InetAddress address;

    AvcEncoder avcCodec;
    public Camera m_camera;
    SurfaceView m_prevewview;
    SurfaceHolder m_surfaceHolder;
    //屏幕分辨率，每个机型不一样，机器连上adb后输入wm size可获取
    private int width = 1920;
    private int height = 1080;
    int framerate = 30;//每秒帧率
    int bitrate = 2500000;//编码比特率，
    private RtpSenderWrapper mRtpSenderWrapper;

    byte[] h264 = new byte[width * height * 3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtp);
        //创建rtp并填写需要发送数据流的地址，直播中需要动态获取客户主动请求的地址
        mRtpSenderWrapper = new RtpSenderWrapper("192.168.137.1", 5500, false);
        avcCodec = new AvcEncoder(width, height, framerate, bitrate);
        m_prevewview = findViewById(R.id.SurfaceViewPlay);
        // 绑定SurfaceView，取得SurfaceHolder对象
        m_surfaceHolder = m_prevewview.getHolder();
        // 预览大小設置
        m_surfaceHolder.setFixedSize(width, height);
        //把输送给surfaceView的视频画面，直接显示到屏幕上,不要维持它自身的缓冲区
        m_surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        m_surfaceHolder.addCallback(this);

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        Log.i("xd----", "MainActivity+surfaceCreated");
        try {
            m_camera = Camera.open();
            m_camera.setDisplayOrientation(90);
            m_camera.setPreviewDisplay(m_surfaceHolder);
            Camera.Parameters parameters = m_camera.getParameters();
            parameters.setPreviewSize(width, height);
            parameters.setPictureSize(width, height);
            parameters.setPreviewFormat(ImageFormat.NV21);//录像时设置了YV12
            m_camera.setParameters(parameters);
            m_camera.setPreviewCallback(this);
            m_camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.i("xd----", "MainActivity+surfaceDestroyed");
        m_camera.setPreviewCallback(null);  //！！这个必须在前，不然退出出错
        m_camera.release();
        m_camera = null;
        avcCodec.close();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        send(data);

    }

    private void send(byte[] data) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int ret = avcCodec.offerEncoder(data, h264);
                if (ret > 0) {
                    Log.i("xdsend----", "开始发送");
                    //实时发送数据流
                    mRtpSenderWrapper.sendAvcPacket(h264, 0, ret, 0);
                }
            }
        }).start();
    }

    private void test(byte[] data) {
        boolean is = true;
        if (is) {
            Log.i("xd---------", "发送");
            byte[] bytes = Arrays.copyOf(data, 1024);
            try {
                DatagramSocket datagramSocket = new DatagramSocket(2345);
                InetAddress localHost = InetAddress.getLocalHost();
                DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, localHost, 5432);
                datagramSocket.send(datagramPacket);
                datagramSocket.close();
                is = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
